/**
 * 所有接口请求
 */
import {
  request,
  getSessionStorage,
} from '/utils/index';

const baseUrl = 'http://xxxxx';

/**
 * userAuth 用户登录
 * @param {string} authCode 字符串
 */
export const userAuth = (authCode) => {
  return request({
    url: `${baseUrl}/openapi/auth?authCode=${authCode}`,
  });
}

/**
 * getActivityTime 获取当前时间及所有活动时间
 */
export const getActivityTime = () => {
  const sst = getSessionStorage();
  return request({
    url: `${baseUrl}/webapi/activity/time`,
    headers: {
      ...sst,
    }
  });
}

/**
 * getUserAwardInfo 请求当前用户是否获奖
 * @param {string} activityTime 活动时间
 */
export const getUserAwardInfo = (activityTime) => {
  const sst = getSessionStorage();
  return new Promise((resolve, reject) => {
    my.httpRequest({
      url: `${baseUrl}/webapi/activity/award?activityTime=${activityTime}`,
      headers: {
        'Content-Type': 'application/json;charset=UTF-8',
        ...sst,
      },
      success: (res) => {
        resolve(res);
      },
      fail: (res) => {
        resolve(res);
      }
    });
  });
}

/**
 * getAllAwardRecords 查询所有获奖记录
 * @param {number} pageSize 数量
 */
export const getAllAwardRecords = (pageSize) => {
  const sst = getSessionStorage();
  return request({
    url: `${baseUrl}/webapi/award/records?pageSize=${pageSize}`,
    headers: {
      ...sst,
    },
  });
}

/**
 * getUserAwardRecords 查询当前用户的获奖记录
 */
export const getUserAwardRecords = () => {
  const sst = getSessionStorage();
  return request({
    url: `${baseUrl}/webapi/award/records/self`,
    headers: {
      ...sst,
    },
  });
}

/**
 * awardQualification 查询在活动时间当前用户是否还有抽奖资格
 * @param {string} activityTime 活动时间
 */
export const awardQualification = (activityTime) => {
  const sst = getSessionStorage();
  return request({
    url: `${baseUrl}/webapi/activity/award/qualification?activityTime=${activityTime}`,
    headers: {
      ...sst,
    },
  });
}